package com.itheima.handler;

import com.itheima.exception.BusinessException;
import com.itheima.pojo.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zzw
 * @date 2025/4/27
 * @description 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public Result handleException(Exception e) {
        log.error("服务器发生异常:{}", e.getMessage());
        return Result.error("对不起，服务器异常，请联系管理员！");
    }

    /**
     * 业务异常处理
     *
     * @param e 业务异常
     * @return Result 统一响应结果
     */
    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException e) {
        log.error("业务异常:{}", e.getMessage());
        return Result.error(e.getMessage());
    }
}
